// StreamDriver.cpp


#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <tchar.h>
#include <Dbgapi.h>

HANDLE g_hInstance;

#define BUFSIZE 256
WCHAR achBuffer[BUFSIZE];

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL WINAPI  
DllEntryPoint(HANDLE hinstDLL, 
              DWORD dwReason, 
              LPVOID /* lpvReserved */)
{

    switch(dwReason)
    {
    case DLL_PROCESS_ATTACH:
         g_hInstance = hinstDLL;
        RETAILMSG(1,(TEXT("StreamDriver: DLL_PROCESS_ATTACH\n")));
 		DEBUGMSG(1,(TEXT("DEBUG StreamDriver: ATTACH\n")));
		return TRUE;
    case DLL_THREAD_ATTACH:
        RETAILMSG(1,(TEXT("StreamDriver: DLL_THREAD_ATTACH\n")));
		DEBUGMSG(1,(TEXT("StreamDriver: DLL_THREAD_ATTACH\n")));
        break;
    case DLL_THREAD_DETACH:
        RETAILMSG(1,(TEXT("StreamDriver: DLL_THREAD_DETACH\n")));
		DEBUGMSG(1,(TEXT("StreamDriver: DLL_THREAD_DETACH\n")));
        break;
    case DLL_PROCESS_DETACH:
        RETAILMSG(1,(TEXT("StreamDriver: DLL_PROCESS_DETACH\n")));
		DEBUGMSG(1,(TEXT("StreamDriver: DLL_PROCESS_DETACH\n")));
        break;
#ifdef UNDER_CE
    case DLL_PROCESS_EXITING:
        RETAILMSG(1,(TEXT("StreamDriver: DLL_PROCESS_EXITING\n")));
		DEBUGMSG(1,(TEXT("StreamDriver: DLL_PROCESS_EXITING\n")));
        break;
    case DLL_SYSTEM_STARTED:
        RETAILMSG(1,(TEXT("StreamDriver: DLL_SYSTEM_STARTED\n")));
		DEBUGMSG(1,(TEXT("StreamDriver: DLL_SYSTEM_STARTED\n")));
        break;
#endif
    }

    return TRUE;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL SDV_PreClose(DWORD hOpenContext)
{
//If SDV_PreClose is present, SDV_PreDeinit must also be present or the driver will not load
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_PreClose\n")));
	DEBUGMSG(1,(TEXT("StreamDriver: SDV_PreClose\n")));
    return bRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL SDV_Close(DWORD hOpenContext)
{
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_Close\n")));
	DEBUGMSG(1,(TEXT("StreamDriver: SDV_Close\n")));
    return bRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL SDV_Deinit(DWORD hDeviceContext)
{
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_Deinit\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_Deinit\n")));
	
	return bRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL SDV_PreDeinit(DWORD hDeviceContext)
{
//If SDV_PreClose is present, SDV_PreDeinit must also be present or the driver will not load.
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_PreDeinit\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_PreDeinit\n")));
	
	return bRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD SDV_Init( LPCTSTR pContext, LPCVOID lpvBusContext )
{
    DWORD dwRet = 0;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_Init\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_Init\n")));

    // Initialize buffer to zero.
    memset (achBuffer, 0, BUFSIZE * sizeof(WCHAR));

    // Set return value to non-zero.
    dwRet = 1;
    return dwRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL SDV_IOControl(DWORD hOpenContext, 
                   DWORD dwCode, 
                   PBYTE pBufIn, 
                   DWORD dwLenIn, 
                   PBYTE pBufOut, 
                   DWORD dwLenOut, 
                   PDWORD pdwActualOut)
{
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_IOControl\n")));
	DEBUGMSG(1,(TEXT("StreamDriver: SDV_IOControl\n")));
    return bRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD SDV_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
    DWORD dwRet = 0;
    RETAILMSG(1,(TEXT("RETAIL StreamDriver: SDV_Open\n")));
	DEBUGMSG(1,(TEXT("DEBUG StreamDriver: SDV_Open\n")));

    dwRet = 1; // Have to make non-zero for this call to succeed.
    return dwRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SDV_PowerDown(DWORD hDeviceContext)
{
    RETAILMSG(1,(TEXT("StreamDriver: SDV_PowerDown\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_PowerDown\n"))); 
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SDV_PowerUp(DWORD hDeviceContext)
{
    RETAILMSG(1,(TEXT("StreamDriver: SDV_PowerUp\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_PowerUp\n"))); 
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD SDV_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
    DWORD dwRet = 0;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_Read\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_Read\n")));

    // Copy the smaller of buffer size or string size.
    DWORD cbBuffer = wcslen( achBuffer) + 1;
    dwRet = min(cbBuffer, Count);
    wcsncpy((LPWSTR)pBuffer, achBuffer, dwRet);

    // Return number of bytes read.
    return dwRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD SDV_Seek(DWORD hOpenContext, long Amount, DWORD Type)
{
    DWORD dwRet = 0;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_Seek\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_Seek\n")));
	return dwRet;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD SDV_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
{
    DWORD dwRet = 0;
    RETAILMSG(1,(TEXT("StreamDriver: SDV_Write\n")));
    DEBUGMSG(1,(TEXT("StreamDriver: SDV_Write\n")));
    // Copy the smaller of buffer size or number of bytes they send us.
    dwRet = min(BUFSIZE, NumberOfBytes);
    wcsncpy(achBuffer, (LPWSTR)pSourceBytes, dwRet);

    // Return number of bytes written.
    return dwRet;
}



